#include "data_global.h"
#include "sqlite_link_list.h"
extern pthread_mutex_t mutex_client_request;
extern pthread_cond_t cond_client_request;
extern int msgid;
extern pthread_cond_t cond_led;
extern pthread_cond_t cond_buzzer;
extern pthread_cond_t cond_camera;
extern pthread_cond_t cond_uart_cmd;
extern unsigned char dev_buzzer_mask;
extern unsigned char dev_led_mask;
extern unsigned char dev_camera_mask;
extern unsigned char dev_uart_mask; 
extern pthread_mutex_t mutex_sqlite;

extern int updateEnv(struct env_info_clien_addr env_info_clien_addr_t, int storageNum_t);

extern pthread_mutex_t mutex_slinklist;
extern pthread_cond_t cond_analysis;
extern slinklist slinkHead;
extern int sqlite_InsertLinknode(slinklist,struct env_info_clien_addr,int ,int);
extern pthread_cond_t cond_sqlite;
extern struct sqlite_operation sqlite_operation_t;


struct setEnv{
	int temMAX;
	int temMIN; //10-50
	int humMAX;
	int humMIN; 
	int illMAX;
	int illMIN;
};
unsigned char buzzer_mask;
void *pthread_client_request(void*msg)
{
	puts("pthread_client_request ok!!!");
	struct setEnv getEnv;
	struct msg recv_buf;
	key_t key;
	struct env_info_clien_addr K;
	if((key = ftok("/app", 'g')) < 0)
	{
		perror("ftok");
		exit(1);
	}

	if((msgid = msgget(key, IPC_CREAT | 0666)) == -1)
	{
		printf("errno = %d\n",errno);
		perror("msgget 1");
		exit(1);
	}
	pthread_mutex_lock(&mutex_client_request);
	while(1)
	{
		//msgid全局   去匹配发送端id接收   匹配1L 拿数据
//		printf("%d\n",msgid);
//		printf("%d\n",msgid);
		if(msgrcv(msgid,&recv_buf,(sizeof(struct msg)-sizeof(long)),1L,0)==-1)  //接收消息，没有則阻塞
		{
			perror("msgrcv");
			break;
		}
		switch(recv_buf.msgtype){
			case 1L:
				{
					puts("led");
					pthread_cond_signal(&cond_led); //唤醒led
					dev_led_mask= recv_buf.text[0];
					break;
				}
			case 2L:
				{
					puts("buzzer");
					pthread_cond_signal(&cond_buzzer);//唤醒buzzer
					dev_buzzer_mask= recv_buf.text[0];
					break;
				}
			case 3L:
				{
					puts("camera");
					pthread_cond_signal(&cond_camera);//唤醒camera截图
					dev_camera_mask= recv_buf.text[0];
					break;
				}
			case 4L:
					puts("uart");
					pthread_cond_signal(&cond_uart_cmd);//唤醒uart_cmd
					buzzer_mask = dev_uart_mask= recv_buf.text[0];
					break;
#if 1
			case 5L:
				{
					puts("---------*****************************************-----55555555---------------5L");
				//	pthread_cond_signal(&cond_);//唤醒buzzer
				//	setLimit(recv_buf.text[0],recv_buf.text[1],recv_buf.text[2],recv_buf.text[3],recv_buf.text[4],recv_buf.text[5],recv_buf.text[6]);
					memcpy(&getEnv,recv_buf.text+1,24);
					puts("---------*****************************************-----55555555---------------5L");

					int sto_no = recv_buf.text[0]-'0';

					K.storage_no[sto_no].temperatureMAX=(float)getEnv.temMAX;
					K.storage_no[sto_no].temperatureMIN=(float)getEnv.temMIN;
					K.storage_no[sto_no].humidityMAX=(float)getEnv.humMAX;
					K.storage_no[sto_no].humidityMIN=(float)getEnv.humMIN;
					K.storage_no[sto_no].illuminationMAX=(float)getEnv.illMAX;
					K.storage_no[sto_no].illuminationMIN=(float)getEnv.illMIN;

#if 1
					pthread_mutex_lock(&mutex_sqlite);

					sqlite3 *db;
					sqlite3_open("warehouse", &db);
					updateEnv(K, sto_no);
					usleep(10000);
					sqlite3_close(db);

					pthread_mutex_unlock(&mutex_sqlite);
					puts("************************************************");
					
				//	pthread_mutex_lock(&mutex_slinklist);
#endif
					//sqlite_operation_t.table_select_mask = ENV_OP_MASK;
					//sqlite_operation_t.env_operation_mask = COLLECT_INSERTER;
#if 0
					sqlite_InsertLinknode(slinkHead,K,recv_buf.text[0],1);

					pthread_cond_signal(&cond_sqlite);

					pthread_mutex_unlock(&mutex_slinklist);
#endif
				//	pthread_cond_signal(&cond_analysis);//唤醒数据处理线程

				//	setLimit(recv_buf.text[0],getEnv.temMAX,getEnv.temMIN,getEnv.humMAX,getEnv.humMIN,getEnv.illMAX,getEnv.illMIN);
					break;
				}
#endif
			default:
				break;
		}
		pthread_mutex_unlock(&mutex_client_request);
	}
	exit(0);
}
